package programmercarl.动态规划.C7;

class Solution {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int[][] dp = new int[obstacleGrid.length][obstacleGrid[0].length];

        if (obstacleGrid[0][0] == 1 || obstacleGrid[obstacleGrid.length - 1][obstacleGrid[0].length - 1] == 1) {
            return 0;
        }
        for (int i = 0; i < obstacleGrid.length&& obstacleGrid[i][0] == 0; i++) {//这里可以让其上方或者左方有1的后续都为0
                dp[i][0] = 1;
        }
        for (int j = 0; j < obstacleGrid[0].length&& obstacleGrid[0][j] == 0; j++) {
                dp[0][j] = 1;
        }
        for (int i = 1; i < obstacleGrid.length; i++) {
            for (int j = 1; j < obstacleGrid[0].length; j++) {
                dp[i][j] = (obstacleGrid[i][j] == 0) ? dp[i - 1][j] + dp[i][j - 1] : 0;
            }
        }
        return dp[obstacleGrid.length-1][obstacleGrid[0].length-1];
    }
}